package com.hnucm.cooklife.presenter.dishresult;

import androidx.annotation.NonNull;

import com.hnucm.cooklife.base.BasePresenter;
import com.hnucm.cooklife.bean.db.Dish;
import com.hnucm.cooklife.contract.dishresult.Contract;
import com.hnucm.cooklife.model.DishResultModel;

import java.util.List;
import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class DishResultPresenter extends BasePresenter<Contract.IDishResultView> implements Contract.IDishResultPresenter {
    Contract.IDishResultModel mIDishResultModel;

    public DishResultPresenter() {
        mIDishResultModel = new DishResultModel();
    }

    @Override
    public void loadDishResult(int currentPage, Map<String,String> dishname) {
        mIDishResultModel.loadDishResult(currentPage,dishname)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<Dish>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mCompositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(@NonNull List<Dish> dishList) {
                        if(isViewAttached()){
                            getView().onLoadDishResult(dishList);
                            getView().onLoadSuccess();
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        e.printStackTrace();
                        if(isViewAttached()){
                            getView().onLoadingFailed();
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void refreshDishResult(int currentPage,Map<String,String> dishname) {

    }
}
